﻿using FluorineFx.Json;
using HttpSocket;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace demo_win_httpsocket
{
    partial class MainForm
    {
        void _8_WEBWXSYNC()
        {
            //_ShowMessage(System.Reflection.MethodInfo.GetCurrentMethod().Name);
            var response = WEB.SendRequest(@"POST https://wx{NUMBER}.qq.com/cgi-bin/mmwebwx-bin/webwxsync?sid={WXSID}&skey={SKEY}&lang=zh_CN&pass_ticket={PASS_TICKET} HTTP/1.1
Host: wx.qq.com
Connection: keep-alive
Content-Length: 301
Accept: application/json, text/plain, */*
Origin: https://wx.qq.com
User-Agent: Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/47.0.2526.106 Safari/537.36
Content-Type: application/json;charset=UTF-8
Referer: https://wx.qq.com/?&lang=zh_CN
Accept-Encoding: gzip, deflate
Accept-Language: zh-CN,zh;q=0.8
Cookie: webwxuvid=bebe27f97a88e8ce573a38b4c48984e8f578650b8ba48b2bacce4ec572e41a3f2b1d021c6656cfeef62091ed7ac2c000; o_cookie=7103505; pgv_pvid=4255253068; pt_clientip=4c950af78b5036b4; pt_serverip=feef0abf0662c02f; ptui_loginuin=7103505; ptcz=6a354e61d971ccc416a5d9fa461f67c599f02d19c0bbb5cc8dad6d82c7850721; pt2gguin=o0007103505; uin=o0007103505; skey=@SVm04rrY6; qm_username=7103505; qm_sid=65f2ed6111bef5d92e7dbf710aec3449,qb3BUenJOMmtmUndFenl3MVFmaExsQkJOYmpVLWRqeC1qNG0xeGxGdDZVUV8.; pgv_pvi=9815860224; pgv_si=s1555394560; MM_WX_NOTIFY_STATE=1; MM_WX_SOUND_STATE=1; wxuin=840648442; wxsid=BJONNeerx+IW+mmH; wxloadtime=1452518428; mm_lang=zh_CN; webwx_data_ticket=AQfiPZdvOnP2C2jMbzfG7DN/



",
@"{""BaseRequest"":{""Uin"":{WXUIN},""Sid"":""{WXSID}"",""Skey"":""{SKEY}"",""DeviceID"":""{DEVICEID}""},""SyncKey"":{SYNCKEY_LONG},""rr"":{TIME}}"
);
            _8_Response(response);
        }

        void _8_Response(LxwResponse o)
        {
            var value = o.Value;
            if (value.IndexOf("\"SyncKey\": ") == -1)
                throw new Exception("SyncKey 没有捕获到");

            var retJSON = JavaScriptConvert.DeserializeObject(value) as JavaScriptObject;

            JavaScriptObject obj = retJSON["SyncKey"] as JavaScriptObject;
            if (obj != null && obj.ContainsKey("Count"))
            {
                WEB.Add(SYNCKEY_LONG, JavaScriptConvert.SerializeObject(retJSON["SyncKey"]));
            }

            JavaScriptArray arr = retJSON["AddMsgList"] as JavaScriptArray;
            foreach (JavaScriptObject json in arr)
            {
                var content = json["Content"].ToString()
                    .Replace("&gt;", ">")
                    .Replace("&lt;", "<")
                    .Replace("<br/>", "");

                //处理消息
                _9_DoMessage(content, json, json["MsgId"] + "");
            }
        }
    }
}
